@#define sticky_prices = 1
@#define CRRA = 0
@#define true_stochastic_steady_state_IRFs = 1

var C                                                                   (long_name='consumption')
    M                                                                   (long_name='Discount Factor') 
    N_m                                                                 (long_name='Market Hours')
    P_E                                                              	(long_name='Real price of shares')
    D_E                                        	                        (long_name='Real Dividends of Shares')
    R_R                                                                 (long_name='Risk-Free Rate')
    W                                                                   (long_name='Real Wage')
    V                                                                   (long_name='Value Function')
    E_t_V_tp1_1_minus_sigma                                             (long_name='Auxiliary Variable for EZ')
    Y                                                                   (long_name='Output')
    K_m                                                                 (long_name='Market Capital')
    I_m                                                                 (long_name='Market Capital Investment')
    D                                                                   (long_name='Cash Flows')
    Pi                                                                  (long_name='Unflation')
    R_K                                                                 (long_name='Marginal Revenue Product of Capital')
    q                                                                   (long_name='Tobins q')
    mu                                                                  (long_name='Markup')
    sigma_a                                                             (long_name='Preference Shock volatility')
    a                                                                   (long_name='Preference Shock Level')
    Z                                                                   (long_name='Technology Shock Level')
    R                                                                   (long_name='Policy Rate')
    Xi                                                                  (long_name='Marginal Cost')
    u                                                                   (long_name='Market-Capital Capacity Utilization')
    PHI                                                                 (long_name='Rotemberg Price Adjustment Costs')
    sigma_z                                                             (long_name='TFP Volatility')
    PF_normalization                                                    (long_name='TFP')
    C_m                                                                 (long_name='Market Consumption')
    C_n                                                                 (long_name='Non Market Consumption')
    L                                                                   (long_name='Leisure')
    N_n                                                                 (long_name='Non-Market hours')
    I_n                                                                 (long_name='Non-Market Capital Investment')
    K_n                                                                 (long_name='Non-Market Capital Capital')
    q_n                                                                 (long_name='Relative Price Non-Market Capital')
    u_n                                                                 (long_name='Non-Market Capital Capacity Utilization')
    util                                                                (long_name='Period Utility')
    utilC                                                               (long_name='Marginal Utility of Consumption')
    utilL                                                               (long_name='Marginal Utility of Leisure')
    Lambda                                                              (long_name='Marginal Utility Wealth')
    alpha1_ex                                                           (long_name='Consumption Share shock')
    aux_sigma                                                           (long_name='Auxiliary variable')
    R_E                                                                 (long_name='Auxiliary variable EZ')     
    E_R_E                                                               (long_name='Auxiliary variable EZ')   
    E_R_E_squared                                                       (long_name='Auxiliary variable EZ')  
    cond_var_R_E                                                        (long_name='VXO')
;

varexo 
   eps        ${\varepsilon^a}$                                         (long_name='Level Shock')
   eps_sigma     ${\varepsilon^{\sigma^a}}$                             (long_name='Volatility Shock')
   eps_z      ${\varepsilon^a}$                                         (long_name='Level Shock')
;

parameters siggma                                                       (long_name='risk aversion')
    theta_v                                                             (long_name='uncertainty resolution preference')
    psii                                                                (long_name='intertemporal elasticity of substitution')
    eta                                                                 (long_name='share of consumption in Cobb-Douglas aggregator')
    betta                                                               (long_name='discount factor')
    theta_mu                                                            (long_name='demand elasticity')
    alppha                                                              (long_name='labor share')
    Psi                                                                 (long_name='fixed costs')
    delta_0                                                             (long_name='ss depreciation')
    delta_1                                                             (long_name='variable cu')
    delta_2                                                             (long_name='variable cu')
    phi_k                                                               (long_name='capital adjustment costs')
    phi_p                                                               (long_name='price adjustment costs')
    Pi_bar                                                              (long_name='steady state inflation')
    nu                                                                  (long_name='share of bonds in capital')
    rho_r                                                               (long_name='interest smoothing')
    rho_pi                                                              (long_name='inflation feedback')
    rho_y                                                               (long_name='output growth feedback')
    log_R_bar                                                           (long_name='SS net interest rate')
    sigma_a_bar                                                         (long_name='SS volatility preference shock')
    sigma_sigma_a                                                       (long_name='SS volatility of uncertainty shock')
    rho_a                                                               (long_name='persistence preference shock')
    rho_sigma_a                                                         (long_name='pesristence volatility shock')
    a_bar                                                               (long_name='SS preference shock')
    sigma_z_bar                                                         (long_name='SS volatility technology shock')
    rho_z                                                               (long_name='persistence technology shock')
    V_normalization                                                     (long_name='Normalization value function')
    sigma_eps_m                                                         (long_name='monetary policy shock volatility')
    subsidy                                                             (long_name='steady-state subsidy')
    sigma_sigma_z                                                       (long_name='TFP volatility')
    rho_sigma_z                                                         (long_name='persistence TFP volatility shock')
    PF_normalization_bar                                                (long_name='normalization steady-state output')
    b1P                                                                 (long_name='Home producction elasticity paramater')
    alpha1P                                                             (long_name='Market consumption share')
    alpha2P                                                             (long_name='Home capital share (not used)')
    delta_n_0                                                           (long_name='Home capital utilization (not used)')
    delta_n_1                                                           (long_name='Home capital utilization (not used)')
    delta_n_2                                                           (long_name='Home capital utilization (not used)')
    phi_k_n                                                             (long_name='Home capital adjustment cost (not used)')
    fixed_homepI                                                        (long_name='Indicator')
    home_capitalI                                                       (long_name='Indicator')
    kuI                                                                 (long_name='Indicator')
    ezI                                                                 (long_name='Indicator')
    capitalI                                                            (long_name='Indicator')
    C_n_bar                                                             (long_name='Steady-state value')                                                                                                                                
    N_n_bar                                                             (long_name='Steady-state value')                                                                   
    I_n_bar                                                             (long_name='Steady-state value')                                                                   
    K_n_bar                                                             (long_name='Steady-state value')                                                                   
    q_n_bar                                                             (long_name='Steady-state value')                                                                   
    u_n_bar                                                             (long_name='Steady-state value')    
    W_bar                                                               (long_name='Steady-state value')  
    C_m_bar                                                             (long_name='Steady-state value')  
    C_bar                                                               (long_name='Steady-state value')  
    alpha1I                                                             (long_name='Indicator')
    uncertainty_shockI                                                  (long_name='Indicator')
;

betta              = 0.994;
delta_0            = 0.025;
delta_2            = 0.00031;
@#if sticky_prices == 1
    phi_p = 100;
@#else
    phi_p =0;
@# endif
Pi_bar             = 1.005;
log_R_bar          = log(Pi_bar/betta);
rho_r              = 0;
rho_pi             = 1.5;
rho_y              = 0.2;
psii               = 0.95;
theta_mu          = 6;
a_bar             = 1;
phi_k             = 2.0901;
rho_a             = 0.93564;
sigma_a_bar       = 0.0026251;

rho_sigma_a       = 0.74227;
sigma_sigma_a     = 0.0025022;

sigma_z_bar       = 0.0012857; //this is for labor augmenting-TFP (Basu Bundick)
rho_z             = 0.98793; //this is for labor augmenting-TFP (Basu Bundick)
sigma_sigma_z     = 0.42;                                 
rho_sigma_z       = 0.63;    
nu                = 0.9; //irrelevant due to Modigliani-Miller
sigma_eps_m       = 0;
subsidy           = 1;
b1P               = 0.5;
phi_k_n           = phi_k; 
delta_n_0         = delta_0;
delta_n_2         = delta_2;

model;
 

[name='Welfare function']
V = ezI*((V_normalization*a*(util^((1-siggma)/theta_v)) + betta*(E_t_V_tp1_1_minus_sigma)^(1/theta_v))^(theta_v/(1-siggma)))
    + (1-ezI)*(V_normalization*a*(util^(1-siggma))/(1-siggma) + betta*V(+1));

//[name='Discount Factor']
M = ezI*(betta*(a/a(-1))*((util/util(-1))^((1-siggma)/theta_v - 1))*(utilC/utilC(-1))*((C_m*C(-1)/(C_m(-1)*C))^(b1P-1))*(((V^(1-siggma))/E_t_V_tp1_1_minus_sigma(-1))^(1-1/theta_v)))
    + (1-ezI)*betta*(a(+1)/a)*(utilC(+1)/utilC)^(1-siggma)*(C/C(+1));

[name='Auxiliary variable needed for Epstein-Zin preferences']
E_t_V_tp1_1_minus_sigma=V(+1)^(1-siggma);

[name='Budget constraint']
0 = capitalI*(- (C_m + I_n +P_E+1/R_R*nu*K_m) + W*N_m+(D_E+P_E)+nu*K_m(-1))
    + (1-capitalI)*(- C_m  + Y);

//[name='(10) labor FOC']
W = (utilL/utilC)*(1/(alpha1P*exp(alpha1_ex)))*((C_m/C)^(1-b1P));

[name='(11) Stock FOC']
P_E=M(+1)*(D_E(+1)+P_E(+1));

[name='(12) Firm Bond FOC']
1=R_R*M(+1);

[name='Production function']
Y=exp(PF_normalization)*(u*K_m(-1))^alppha*(Z*N_m)^(1-alppha)-Psi;

[name='LOM Capital']
K_m= capitalI*((1-(delta_0 + delta_1*(u-1)+delta_2/2*(u-1)^2)-phi_k/2*(I_m/K_m(-1)-delta_0)^2)*K_m(-1)+I_m)
     +(1-capitalI);

[name='Cash flows']
D= Y-W*N_m-I_m-phi_p/2*(Pi/Pi_bar-1)^2*Y;

[name='Labor FOC']
W*N_m=(1-alppha)*Xi*exp(PF_normalization)*(u*K_m(-1))^alppha*(Z*N_m)^(1-alppha);

[name='Capital FOC']
R_K*(u*K_m(-1))=alppha*Xi*exp(PF_normalization)*(u*K_m(-1))^alppha*(Z*N_m)^(1-alppha);

[name='Utilization FOC']
0 = capitalI*(kuI*(-q*(delta_1+delta_2*(u-1))*u*K_m(-1)+ alppha*Xi*exp(PF_normalization)*(u*K_m(-1))^alppha*(Z*N_m)^(1-alppha))+ (1-kuI)*(u - 1))
    +(1-capitalI)*(u - 1);

[name='Pricing FOC']
phi_p*(Pi/Pi_bar-1)*(Pi/Pi_bar)=(1-theta_mu)+theta_mu*Xi+phi_p*
    M(+1)*(Y(+1)/Y)*(Pi(+1)/Pi_bar-1)*(Pi(+1)/Pi_bar);

[name='FOC capital']
q= capitalI*(M(+1)*(u(+1)*R_K(+1)+q(+1)*(1-(delta_0 + delta_1*(u(+1)-1)+delta_2/2*(u(+1)-1)^2)-phi_k/2*(I_m(+1)/K_m-delta_0)^2 +phi_k*(I_m(+1)/K_m-delta_0)*(I_m(+1)/K_m))))
   +(1-capitalI);

[name='FOC investment']
1/q=capitalI*(1-phi_k*(I_m/K_m(-1)-delta_0))
    +(1-capitalI)*(1/q - I_m);

[name='Dividends']
D_E=capitalI*(D-nu*(K_m(-1)-1/R_R*K_m))
    +(1-capitalI)*D;

[name='Taylor Rule']
log(R)=rho_r*log(R(-1))+(1-rho_r)*(log_R_bar+rho_pi*(log(Pi)-log(Pi_bar))+rho_y*(log(Y)-log(Y(-1))));//+sigma_eps_m*eps;

[name='Bond FOC']
1=betta*(a(+1)/a)*((util(+1)/util)^((1-siggma)/theta_v - 1))*(utilC(+1)/utilC)*((C_m(+1)*C/(C_m*C(+1)))^(b1P-1))*(((V(+1)^(1-siggma))/E_t_V_tp1_1_minus_sigma)^(1-1/theta_v))*(R/Pi(+1));

[name='Markup definition']
mu=1/Xi;

[name='Rotemberg cost definition']
PHI=1+phi_p/2*(Pi/Pi_bar-1)^2*Y;

[name='Preference shock process level equation']
a=(1-rho_a)*a_bar+rho_a*a(-1)+sigma_a(-1)*eps*uncertainty_shockI;


[name='Preference shock volatility process']
sigma_a=(1-rho_sigma_a)*sigma_a_bar+rho_sigma_a*sigma_a(-1)+sigma_sigma_a*eps_sigma*uncertainty_shockI;

[name='TFP shock process']
Z=(1-rho_z)*1+rho_z*Z(-1)-sigma_z_bar*eps_z;

[name='TFP shock process']
PF_normalization = rho_z*PF_normalization(-1) + (1-rho_z)*PF_normalization_bar;

[name='TFP volatility shock process']
sigma_z = (1-rho_sigma_z)*sigma_z_bar + rho_sigma_z*sigma_z(-1);

[name='Period utility']
util = C^eta*(L^(1-eta));

[name='Marginal Utility of Consumption']
utilC = eta*(C^(eta-1))*(L^(1-eta));

[name='Marginal Disutility of Labor']
utilL = (1-eta)*(C^eta)*(L^-eta);

[name='Total Consumption']
C = ((alpha1P*exp(alpha1_ex))*(C_m^b1P) + (1-(alpha1P*exp(alpha1_ex)))*(C_n^b1P))^(1/b1P) ;

[name='Hours Worked']
L = 1 - N_m - N_n;

[name='Non-Market Consumption']
C_n = (1-fixed_homepI)*capitalI*((u_n*K_n(-1))^alpha2P)*(N_n^(1-alpha2P))
     + (1-fixed_homepI)*(1-capitalI)*(N_n^(1-alpha2P))
    + fixed_homepI*(C_n_bar);

[name='Non-Market Capital']
K_n = (1-fixed_homepI)*home_capitalI*((1-(delta_n_0 + delta_n_1*(u_n-1)+delta_n_2/2*(u_n-1)^2)-phi_k_n/2*(I_n/K_n(-1)-delta_n_0)^2)*K_n(-1)+I_n)
    + (fixed_homepI + (1-fixed_homepI)*(1-home_capitalI))*K_n_bar;

[name='Non-Market Capital Utilization']
0   = kuI*(1-fixed_homepI)*home_capitalI*(- q_n*u_n*(delta_n_1+delta_n_2*(u_n-1)) + alpha2P*((1-(alpha1P*exp(alpha1_ex)))/(alpha1P*exp(alpha1_ex)))*(C_n/K_n(-1))*((C_n/C_m)^(b1P-1)))
    + (fixed_homepI + (1-fixed_homepI)*(1-home_capitalI))*(u_n - u_n_bar);

[name='Tobin q Non-Market Capital Investment']
0 = (1-fixed_homepI)*home_capitalI*(- q_n + q_n(+1)*M(+1)*(u_n(+1)*(delta_n_1+delta_n_2*(u_n(+1)-1)) + (1-(delta_n_0 + delta_n_1*(u_n(+1)-1)+delta_n_2/2*(u_n(+1)-1)^2)-phi_k_n/2*(I_n(+1)/K_n-delta_n_0)^2 +phi_k_n*(I_n(+1)/K_n-delta_n_0)*(I_n(+1)/K_n))))
    + (fixed_homepI + (1-fixed_homepI)*(1-home_capitalI))*(I_n - I_n_bar);

[name='Non-Market Hours']
0 = (1-fixed_homepI)*((1-(alpha1P*exp(alpha1_ex)))*(1-alpha2P)*((C_n/C)^(b1P - 1))*(C_n/N_n) - utilL/utilC)
    + fixed_homepI*(N_n - N_n_bar);

[name='Home production capital utilization']
0 = (1-fixed_homepI)*home_capitalI*(- 1/q_n + 1-phi_k_n*(I_n/K_n(-1)-delta_n_0))
    + (fixed_homepI + (1-fixed_homepI)*(1-home_capitalI))*(q_n - q_n_bar);

[name='Marginal utility of wealth']
Lambda = ezI*((V/util)^(1-(1-siggma)/theta_v)*utilC*(alpha1P*exp(alpha1_ex))*(C_m/C)^(b1P-1)*a)
         +(1-ezI)*(util^-siggma)*utilC;


[name='Exogenous market consumption share']
alpha1_ex = -(aux_sigma - sigma_a_bar);


[name='Auxiliary variable']
aux_sigma  = ((1-rho_sigma_a)*sigma_a_bar+rho_sigma_a*aux_sigma(-1)+0.01*eps)*alpha1I*(1-uncertainty_shockI) 
+ ((1-rho_sigma_a)*sigma_a_bar+rho_sigma_a*aux_sigma(-1)+sigma_sigma_a*4*eps_sigma*uncertainty_shockI*alpha1I)*uncertainty_shockI*alpha1I
+ sigma_a_bar*(1-alpha1I);


[name='Auxiliary variable']
R_E=(D_E + P_E)/P_E(-1);

[name='Auxiliary variable']
E_R_E=R_E(+1);

[name='Auxiliary variable']
E_R_E_squared=R_E(+1)^2;

[name='VXO']
cond_var_R_E=E_R_E_squared-E_R_E^2;


end;


shocks;
var eps; stderr 1;
var eps_sigma; stderr 1;
var eps_z; stderr 1;

end;

steady;
resid(1);


stoch_simul(order =3,irf=0,noprint,nograph,nomoments,k_order_solver);


